import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flu_framework/http/http_utils.dart';
import 'package:open_file_plus/open_file_plus.dart';
import 'package:path_provider/path_provider.dart';

/**
 * 1.下载文档
 * 2.判断文档是否下载
 * 3.跳转第三方打开文档
 */
class OpenDocumentUtil{
  factory OpenDocumentUtil() => _singleton;
  OpenDocumentUtil._(){
  }

  static final OpenDocumentUtil _singleton = OpenDocumentUtil._();

  static OpenDocumentUtil get instance => OpenDocumentUtil();

  /// 下载文档
  void downloadDocument(String url,String docName,{DownloadProgress? downloadProgress,DownloadCompletion? downloadCompletion,Fail? fail}) async{
    var dir = await getApplicationDocumentsDirectory();
    String savePath = "${dir?.path}/${docName}";
    print("savePath:$savePath");
    CancelToken cancelToken = new CancelToken();
    HttpUtils.download(
        url,
        savePath,
        cancelToken:cancelToken,
        onDownloadProgress: downloadProgress,
        onDownloadCompletion: downloadCompletion,
        onFail: fail
    );
  }

  /// 判断文件是否存在
  Future<bool> checkDocExist(String docName) async {
    var dir = await getApplicationDocumentsDirectory();
    File file = File("${dir?.path}/${docName}");
    return file.exists();
  }

  /// 第三方应用打开文档
  openDoc(String docName) async {
    var dir = await getApplicationDocumentsDirectory();
    OpenFile.open("${dir?.path}/${docName}");
  }


}